//
//  AppDelegate.m
//  StockNet
//
//  Created by 小焘无量 on 2023/1/5.
//

#import "AppDelegate.h"
#import "AppDelegate+SDKSet.h"
#import "AppDelegate+UISet.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [self configSDK];
    
    [self configUI];
    
    [self reloadToken];
    
    return YES;
}

- (void)reloadToken {
    
    
    YTClient *shared = [YTClient shared];
    
    // 未登录不需要刷新token
    if (!shared.isLogin) return;
    
    // 若已登录，发送心跳
    [[YTClient shared] heartbeatStrar];
    
    // 未过期不需要刷新token
    NSInteger nowTime = [[NSDate date] timeIntervalSince1970] * 1000;
    if (nowTime <= shared.user.expires_time_stamp) return;
    
    // 刷新token
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:@"refresh_token" forKey:@"grant_type"];
    [dic setObject:@"all" forKey:@"scope"];
    [dic setObject:shared.user.refresh_token forKey:@"refresh_token"];
    
    [[APIManager manager] Post:URL_Login parameters:dic success:^(id  _Nonnull responseObject) {
        if (NET_RESULT_CODE == 200) {
            [[YTClient shared] loginInWithData:responseObject];
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

@end
